<template>
  <div class="article-page" v-loading="articlesStore.loading">
    <!-- 文章内容 -->
    <article v-if="currentArticle" class="article-content">
      <!-- 文章头部 -->
      <header class="article-header">
        <div class="container">
          <!-- 面包屑导航 -->
          <el-breadcrumb class="article-breadcrumb">
            <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="`/category/${currentArticle.category}`">
              {{ currentArticle.category }}
            </el-breadcrumb-item>
            <el-breadcrumb-item>{{ truncateText(currentArticle.title, 30) }}</el-breadcrumb-item>
          </el-breadcrumb>

          <!-- 优先级标签 -->
          <div v-if="currentArticle.priority !== 'normal'" class="priority-section">
            <el-tag 
              :type="getPriorityStyle(currentArticle.priority).type" 
              size="large"
              class="priority-tag"
            >
              {{ getPriorityStyle(currentArticle.priority).text }}
            </el-tag>
          </div>

          <!-- 文章标题 -->
          <h1 class="article-title gradient-text">
            {{ currentArticle.title }}
          </h1>

          <!-- 文章元信息 -->
          <div class="article-meta">
            <div class="author-section">
              <el-avatar :size="48" :src="currentArticle.author.avatar" />
              <div class="author-info">
                <h3 class="author-name">{{ currentArticle.author.name }}</h3>
                <p class="author-title">{{ currentArticle.author.title }}</p>
              </div>
            </div>

            <div class="meta-stats">
              <div class="stat-item">
                <el-icon><Clock /></el-icon>
                <span>{{ formatRelativeTime(currentArticle.publishTime) }}</span>
              </div>
              <div class="stat-item">
                <el-icon><View /></el-icon>
                <span>{{ formatNumber(currentArticle.readCount) }} 阅读</span>
              </div>
              <div class="stat-item">
                <el-icon><Timer /></el-icon>
                <span>{{ currentArticle.readingTime }} 分钟阅读</span>
              </div>
            </div>
          </div>

          <!-- 标签列表 -->
          <div class="article-tags">
            <el-tag
              v-for="tag in currentArticle.tags"
              :key="tag"
              class="article-tag"
              :style="{ '--tag-color': getRandomColor() }"
            >
              {{ tag }}
            </el-tag>
          </div>
        </div>
      </header>

      <!-- 封面图片 -->
      <div class="article-cover">
        <el-image
          :src="currentArticle.coverImage"
          :alt="currentArticle.title"
          fit="cover"
          class="cover-image"
        >
          <template #placeholder>
            <div class="image-placeholder">
              <el-icon size="48"><Picture /></el-icon>
            </div>
          </template>
        </el-image>
      </div>

      <!-- 文章正文 -->
      <div class="article-body">
        <div class="container">
          <div class="content-wrapper">
            <!-- 摘要 -->
            <div class="article-summary glass-effect">
              <h3>摘要</h3>
              <p>{{ currentArticle.summary }}</p>
            </div>

            <!-- 正文内容 -->
            <div class="article-text" v-html="formatContent(currentArticle.content)"></div>

            <!-- 互动按钮 -->
            <div class="article-actions">
              <el-button 
                type="primary" 
                size="large" 
                @click="handleLike"
                :class="{ 'liked': isLiked }"
                class="like-button"
              >
                <el-icon><View /></el-icon>
                <span>{{ isLiked ? '已点赞' : '点赞' }} ({{ formatNumber(currentArticle.likeCount) }})</span>
              </el-button>
              
              <el-button 
                size="large" 
                @click="handleShare"
                class="share-button"
              >
                <el-icon><Share /></el-icon>
                <span>分享文章</span>
              </el-button>
              
              <el-button 
                size="large" 
                @click="scrollToTop"
                class="back-top-button"
              >
                <el-icon><Top /></el-icon>
                <span>回到顶部</span>
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </article>

    <!-- 推荐文章 -->
    <section v-if="articlesStore.recommendedArticles.length > 0" class="recommended-section">
      <div class="container">
        <h2 class="section-title">
          <el-icon><Star /></el-icon>
          相关推荐
        </h2>
        <div class="recommended-grid">
          <NewsCard 
            v-for="article in articlesStore.recommendedArticles" 
            :key="article.id"
            :article="article"
            :compact="true"
            class="recommended-item"
          />
        </div>
      </div>
    </section>

    <!-- 文章未找到 -->
    <div v-else-if="!articlesStore.loading && !currentArticle" class="not-found">
      <div class="container">
        <div class="not-found-content">
          <el-icon size="64"><Warning /></el-icon>
          <h2>文章未找到</h2>
          <p>抱歉，您访问的文章不存在或已被删除。</p>
          <el-button type="primary" @click="goHome">返回首页</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Clock, View, Timer, Picture, Share, Top, Star, Warning 
} from '@element-plus/icons-vue'
import NewsCard from '@/components/NewsCard.vue'
import { useArticlesStore } from '@/stores/articles'
import { 
  formatRelativeTime, 
  formatNumber, 
  truncateText,
  getRandomColor,
  getPriorityStyle,
  scrollToTop,
  copyToClipboard
} from '@/utils'

const route = useRoute()
const router = useRouter()
const articlesStore = useArticlesStore()

const isLiked = ref(false)

const currentArticle = computed(() => articlesStore.currentArticle)
const articleId = computed(() => route.params.id as string)

onMounted(async () => {
  await loadArticle()
})

watch(() => route.params.id, async (newId) => {
  if (newId) {
    await loadArticle()
  }
})

const loadArticle = async () => {
  if (articleId.value) {
    await articlesStore.fetchArticleById(articleId.value)
  }
}

const formatContent = (content: string): string => {
  // 简单的Markdown渲染
  return content
    .replace(/^# (.*$)/gim, '<h1 class="content-h1">$1</h1>')
    .replace(/^## (.*$)/gim, '<h2 class="content-h2">$1</h2>')
    .replace(/^### (.*$)/gim, '<h3 class="content-h3">$1</h3>')
    .replace(/\*\*(.*)\*\*/gim, '<strong>$1</strong>')
    .replace(/\*(.*)\*/gim, '<em>$1</em>')
    .replace(/\n/gim, '<br>')
}

const handleLike = async () => {
  if (!currentArticle.value || isLiked.value) return
  
  isLiked.value = true
  await articlesStore.likeArticle(currentArticle.value.id)
  
  ElMessage({
    message: '点赞成功！',
    type: 'success',
    duration: 1500
  })
}

const handleShare = async () => {
  if (!currentArticle.value) return
  
  const url = window.location.href
  const text = `${currentArticle.value.title} - TechNova科技资讯`
  
  if (navigator.share) {
    try {
      await navigator.share({
        title: text,
        url: url
      })
    } catch (error) {
      console.error('分享失败:', error)
    }
  } else {
    const success = await copyToClipboard(url)
    ElMessage({
      message: success ? '链接已复制到剪贴板' : '分享失败',
      type: success ? 'success' : 'error',
      duration: 1500
    })
  }
}

const goHome = () => {
  router.push('/')
}
</script>

<style scoped lang="scss">
.article-page {
  min-height: 100vh;
  background: var(--tech-bg-primary);
}

.article-header {
  padding: 40px 0;
  background: linear-gradient(135deg, var(--tech-bg-secondary), var(--tech-bg-primary));
  
  .article-breadcrumb {
    margin-bottom: 20px;
    
    :deep(.el-breadcrumb__item) {
      .el-breadcrumb__inner {
        color: var(--tech-text-secondary);
        
        &:hover {
          color: var(--tech-accent-neon);
        }
      }
      
      &:last-child .el-breadcrumb__inner {
        color: var(--tech-text-primary);
      }
    }
  }
  
  .priority-section {
    margin-bottom: 16px;
    
    .priority-tag {
      font-size: 14px;
      padding: 8px 16px;
      border-radius: 20px;
      font-weight: 600;
    }
  }
  
  .article-title {
    font-size: 36px;
    font-weight: 800;
    line-height: 1.3;
    margin-bottom: 32px;
    letter-spacing: -0.5px;
  }
  
  .article-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding: 24px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    backdrop-filter: blur(10px);
    
    .author-section {
      display: flex;
      align-items: center;
      gap: 16px;
      
      .author-info {
        .author-name {
          font-size: 16px;
          font-weight: 600;
          color: var(--tech-text-primary);
          margin-bottom: 4px;
        }
        
        .author-title {
          font-size: 14px;
          color: var(--tech-text-secondary);
        }
      }
    }
    
    .meta-stats {
      display: flex;
      gap: 24px;
      
      .stat-item {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 14px;
        color: var(--tech-text-secondary);
        
        .el-icon {
          color: var(--tech-accent-neon);
        }
      }
    }
  }
  
  .article-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    
    .article-tag {
      background: linear-gradient(135deg, 
        rgba(var(--tag-color), 0.2), 
        rgba(var(--tag-color), 0.1)
      );
      border: 1px solid rgba(var(--tag-color), 0.4);
      color: var(--tech-text-primary);
      font-size: 14px;
      padding: 8px 16px;
      border-radius: 20px;
      transition: all var(--tech-transition-normal) ease;
      
      &:hover {
        background: linear-gradient(135deg, 
          rgba(var(--tag-color), 0.3), 
          rgba(var(--tag-color), 0.2)
        );
        box-shadow: 0 0 12px rgba(var(--tag-color), 0.6);
      }
    }
  }
}

.article-cover {
  height: 400px;
  overflow: hidden;
  
  .cover-image {
    width: 100%;
    height: 100%;
  }
  
  .image-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background: var(--tech-bg-secondary);
    color: var(--tech-text-muted);
  }
}

.article-body {
  padding: 60px 0;
  
  .content-wrapper {
    max-width: 800px;
    margin: 0 auto;
  }
  
  .article-summary {
    padding: 24px;
    margin-bottom: 40px;
    border-radius: 12px;
    border: 1px solid rgba(0, 212, 255, 0.2);
    
    h3 {
      color: var(--tech-accent-neon);
      font-size: 18px;
      margin-bottom: 12px;
    }
    
    p {
      color: var(--tech-text-secondary);
      line-height: 1.6;
      font-size: 16px;
    }
  }
  
  .article-text {
    font-size: 16px;
    line-height: 1.8;
    color: var(--tech-text-primary);
    margin-bottom: 40px;
    
    :deep(.content-h1) {
      font-size: 24px;
      font-weight: 700;
      color: var(--tech-accent-neon);
      margin: 32px 0 16px 0;
      padding-bottom: 8px;
      border-bottom: 2px solid rgba(0, 212, 255, 0.3);
    }
    
    :deep(.content-h2) {
      font-size: 20px;
      font-weight: 600;
      color: var(--tech-text-primary);
      margin: 24px 0 12px 0;
    }
    
    :deep(.content-h3) {
      font-size: 18px;
      font-weight: 600;
      color: var(--tech-text-primary);
      margin: 20px 0 10px 0;
    }
    
    :deep(strong) {
      color: var(--tech-accent-neon);
      font-weight: 600;
    }
    
    :deep(em) {
      color: var(--tech-accent-green);
      font-style: italic;
    }
  }
  
  .article-actions {
    display: flex;
    gap: 16px;
    justify-content: center;
    margin-top: 40px;
    
    .el-button {
      padding: 12px 24px;
      font-size: 16px;
      border-radius: 8px;
      transition: all var(--tech-transition-normal) ease;
      
      &.like-button {
        background: var(--tech-gradient-primary);
        border: none;
        
        &.liked {
          background: var(--tech-gradient-accent);
          animation: pulse 0.6s ease-in-out;
        }
        
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 8px 24px rgba(0, 212, 255, 0.3);
        }
      }
      
      &.share-button, &.back-top-button {
        background: rgba(255, 255, 255, 0.1);
        border: 1px solid rgba(0, 212, 255, 0.3);
        color: var(--tech-text-primary);
        
        &:hover {
          background: rgba(0, 212, 255, 0.2);
          border-color: var(--tech-accent-neon);
          transform: translateY(-2px);
        }
      }
    }
  }
}

.recommended-section {
  padding: 60px 0;
  background: var(--tech-bg-secondary);
  
  .section-title {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 32px;
    color: var(--tech-text-primary);
    
    .el-icon {
      color: var(--tech-accent-neon);
    }
  }
  
  .recommended-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
    
    .recommended-item {
      animation: fadeInUp 0.5s ease forwards;
    }
  }
}

.not-found {
  padding: 100px 0;
  text-align: center;
  
  .not-found-content {
    .el-icon {
      color: var(--tech-text-muted);
      margin-bottom: 24px;
    }
    
    h2 {
      font-size: 24px;
      color: var(--tech-text-primary);
      margin-bottom: 16px;
    }
    
    p {
      font-size: 16px;
      color: var(--tech-text-secondary);
      margin-bottom: 32px;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .article-header {
    padding: 24px 0;
    
    .article-title {
      font-size: 24px;
    }
    
    .article-meta {
      flex-direction: column;
      gap: 16px;
      align-items: flex-start;
      
      .meta-stats {
        gap: 16px;
      }
    }
  }
  
  .article-cover {
    height: 250px;
  }
  
  .article-body {
    padding: 40px 0;
    
    .article-actions {
      flex-direction: column;
      align-items: center;
      
      .el-button {
        width: 100%;
        max-width: 200px;
      }
    }
  }
  
  .recommended-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>